import 'package:dio/dio.dart';
import 'package:flutter_todo_2019/common/user.dart';
import 'package:flutter_todo_2019/net/dio_manager.dart';
import 'package:flutter_todo_2019/page/api/apis.dart';
import 'package:flutter_todo_2019/page/mode/base_model.dart';
import 'package:flutter_todo_2019/page/mode/todo_list_model.dart';
import 'package:flutter_todo_2019/page/mode/user_info_model.dart';
import 'package:flutter_todo_2019/page/mode/user_model.dart';

ApiService _apiService = new ApiService();

ApiService get apiService => _apiService;

class ApiService {
  Options _getOptions() {
    Map<String, String> map = new Map();
    map["Cookie"] = User().cookie.toString();
    return Options(headers: map);
  }

  /// 登录
  void login(Function callback, Function errorCallback, String _username,
      String _password) async {
    FormData formData =
        new FormData.fromMap({"username": _username, "password": _password});
    dio.post(Apis.USER_LOGIN, data: formData).then((response) {
      callback(UserModel.fromJson(response.data), response);
    }).catchError((e) {
      errorCallback(e);
    });
  }

  /// 注册
  void register(Function callback, Function errorCallback, String _username,
      String _password) async {
    FormData formData = new FormData.fromMap({
      "username": _username,
      "password": _password,
      "repassword": _password
    });
    dio.post(Apis.USER_REGISTER, data: formData).then((response) {
      callback(UserModel.fromJson(response.data));
    }).catchError((e) {
      errorCallback(e);
    });
  }

  /// 退出登录
  void logout(Function callback, Function errorCallback) async {
    dio.get(Apis.USER_LOGOUT).then((response) {
      callback(BaseModel.fromJson(response.data));
    }).catchError((e) {
      errorCallback(e);
    });
  }

//
  /// 获取TODO列表数据
  void getTodoList(Function callback, Function errorCallback, int _page) async {
    dio.get(Apis.TODO_LIST + "/$_page/json").then((response) {
      callback(TodoListModel.fromJson(response.data));
    }).catchError((e) {
      errorCallback(e);
    });
  }

  /// 获取未完成TODO列表
  void getNoTodoList(
      Function callback, Function errorCallback, int _type, int _page) async {
    dio.post(Apis.NO_TODO_LIST + "/$_type/json/$_page").then((response) {
      callback(TodoListModel.fromJson(response.data));
    }).catchError((e) {
      errorCallback(e);
    });
  }

  /// 获取已完成TODO列表
  void getDoneTodoList(
      Function callback, Function errorCallback, int _type, int _page) async {
    dio.post(Apis.DONE_TODO_LIST + "/$_type/json/$_page").then((response) {
      callback(TodoListModel.fromJson(response.data));
    }).catchError((e) {
      errorCallback(e);
    });
  }

  /// 新增一个TODO
  void addTodo(Function callback, Function errorCallback, params) async {
    dio.post(Apis.ADD_TODO, queryParameters: params).then((response) {
      callback(BaseModel.fromJson(response.data));
    }).catchError((e) {
      errorCallback(e);
    });
  }

  /// 根据ID更新TODO
  void updateTodo(
      Function callback, Function errorCallback, int _id, params) async {
    dio
        .post(Apis.UPDATE_TODO + "/$_id/json", queryParameters: params)
        .then((response) {
      callback(BaseModel.fromJson(response.data));
    }).catchError((e) {
      errorCallback(e);
    });
  }

  /// 仅更新完成状态Todo
  void updateTodoState(
      Function callback, Function errorCallback, int _id, params) async {
    dio
        .post(Apis.UPDATE_TODO_STATE + "/$_id/json", queryParameters: params)
        .then((response) {
      callback(BaseModel.fromJson(response.data));
    }).catchError((e) {
      errorCallback(e);
    });
  }

  /// 根据ID删除TODO
  void deleteTodoById(
      Function callback, Function errorCallback, int _id) async {
    dio.post(Apis.DELETE_TODO_BY_ID + "/$_id/json").then((response) {
      callback(BaseModel.fromJson(response.data));
    }).catchError((e) {
      errorCallback(e);
    });
  }
//
  /// 获取用户个人信息
  void getUserInfo(Function callback, Function errorCallback) async {
    dio.get(Apis.USER_INFO).then((response) {
      callback(UserInfoModel.fromJson(response.data));
    }).catchError((e) {
      errorCallback(e);
    });
  }

}
